package com.atstudy.generic;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2023/1/3 15:09
 * @copyright: 博为峰
 */
public class GenericDemo03 {
    public static void main(String[] args) {
        ArrayList<Student> arrayList1 = new ArrayList<Student>();
        arrayList1.add(new Student("张三",20));
        arrayList1.add(new Student("李四",30));
//        m1(arrayList1);
        m2(arrayList1);

        ArrayList<Worker> arrayList2 = new ArrayList<Worker>();
        arrayList2.add(new Worker("张三",20));
        arrayList2.add(new Worker("李四",30));
//        m1(arrayList2);
//        m2(arrayList2);

        ArrayList<String> arrayList3 = new ArrayList<String>();
        arrayList3.add("a");
//        m1(arrayList3);
    }

    //下限，接收Student类型或Student的父类型
    private static void m2(ArrayList<? super Student> arrayList) {
        Iterator<?> iterator = arrayList.iterator();

        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    //上限，接收Person类型或子类类型
    private static void m1(ArrayList<? extends Person> arrayList) {
        Iterator<?> iterator = arrayList.iterator();

        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
